package com.albon.soccer;

import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.albon.sportsalarm.R;
import com.albon.util.Logg;

public class Soccer {
	private final String TAG = "Soccer";
	
	private int id;  // 1. key값
	private String enable; //2. 알람 on/off
	private String real_date; //3. 실제 경기일정날짜
	private String real_time; //4. 실제 경기 일정 시간
	private String compare_date; // 5.db와 비교 날짜시간
	private String setting_year; //6. 사용자가 설정한 날짜
	private String setting_month; //7. 사용자가 설정한 시간
	private String reague; //8.리그
	private String stadium; //9.구장 
	private String a_team; //10.a 팀
	private String b_team; //11. b 팀 
	private String score; // 12. 스코어
	private String alarm_title; // 13.알람 타이틀
	private String alarm_ringtone; // 14.알람 링톤 url
	private String alarm_volume; // 15.알람 볼륨
	private String alarm_vibrate; // 16.알람 진동 on/off
	private String alarm_auto_off_time; // 17.알람 자동 종료 시간
	private String sms; //18.문자중계
	private String video; //19.동영상중계
	private String record; //20.경기기록
	private String highlight; //21.하이라이트
	private String power; //22.전력분석
	private String notification; //23.알림
	private String explanation; //24. 설명
	
    private String mYear;
    private String mMonth;
    
	public void setmYear(String mYear) {
		this.mYear = mYear;
	}
	public void setmMonth(String mMonth) {
		this.mMonth = mMonth;
	}
	public String getmYear() {
		return mYear;
	}
	public String getmMonth() {
		return mMonth;
	}
	private boolean isBtnClicked; //알람 체크여부	
	public void setBtnClicked(boolean isBtnClicked) {
		this.isBtnClicked = isBtnClicked;
	}
	public boolean isBtnClicked() {
		return isBtnClicked;
	}
	
	private  String[] notification_time_array_name =  {};
	 
	String jsonData;
    Context mContext;
    //json 파싱 생성자
    public Soccer(Context context, String jsonData, String year, String month, String day){
    	mContext = context;
    	notification_time_array_name = context.getResources().getStringArray(R.array.notification_time_array_name);
	
    	this.jsonData = jsonData;
   
    		try{
    			JSONObject order = new JSONObject(jsonData);    			
    			compare_date = year+month+day;
    			
    			real_date = order.has("date")?order.getString("date"):"";
    			real_time = order.has("time")?order.getString("time"):"";
    			a_team = order.has("a_team")?order.getString("a_team"):"";
    			b_team = order.has("b_team")?order.getString("b_team"):"";
    			score = order.has("score")?order.getString("score"):"";
    			stadium = order.has("place")?order.getString("place"):"";
    			Soccer soccer = call_alarm_values(compare_date, a_team.trim(), b_team.trim());
    			Logg.d(TAG, "compare_date : "+compare_date);
    			
    			if(soccer!=null){
    				enable = soccer.getEnable();
    				int noti = Integer.parseInt(soccer.getNotification());
    				notification = notification_time_array_name[noti];
    			}else{
    				enable = "false";
    				notification = notification_time_array_name[0];
    			}    		
    			
    		}catch(Exception e){
    			Logg.e(TAG, "e : "+e);
    		}

    }
	
	public Soccer(int id, String alarm_title, String enable, String real_date, String real_time, String compare_date, String a_team, String b_team, String score, String notification, String stadium, String reague){
		this.id = id;
		this.alarm_title = alarm_title;
		this.enable = enable;
		this.real_date = real_date;
		this.real_time = real_time;
		this.compare_date = compare_date;
		this.a_team = a_team;
		this.b_team = b_team;
		this.score = score;
		this.notification = notification;
		this.stadium = stadium;
		this.reague = reague;
	}
	
	public Soccer(int id, String enable, String real_date, String real_time, String compare_date, String setting_year, String setting_month, String reague, String stadium, 
			String a_team, String b_team, String score, String alarm_title, String alarm_ringtone, String alarm_volume, String alarm_vibrate, 
			String alarm_auto_off_time, String sms,	String video, String record, String highlight, String power, String notification, String explanation){
		this.id = id;
		this.enable = enable;
		this.real_date = real_date;
		this.real_time = real_time;
		this.compare_date = compare_date;
		this.setting_year = setting_year;
		this.setting_month = setting_month;
		this.reague = reague;
		this.stadium = stadium;
		this.a_team = a_team;
		this.b_team = b_team;
		this.score = score;
		this.alarm_title = alarm_title;
		this.alarm_ringtone = alarm_ringtone;
		this.alarm_volume = alarm_volume;
		this.alarm_vibrate = alarm_vibrate;
		this.alarm_auto_off_time = alarm_auto_off_time;
		this.sms = sms;
		this.video = video;
		this.record = record;
		this.highlight = highlight;
		this.power = power;
		this.notification = notification;
		this.explanation = explanation;
	}

	public int getId() {
		return id;
	}

	public String getEnable() {
		return enable;
	}

	public String getReal_date() {
		return real_date;
	}

	public String getReal_time() {
		return real_time;
	}

	public String getCompare_date() {
		return compare_date;
	}			

	public String getSetting_year() {
		return setting_year;
	}
	
	public String getSetting_month() {
		return setting_month;
	}
	
	public String getReague() {
		return reague;
	}

	public String getStadium() {
		return stadium;
	}

	public String getA_team() {
		return a_team;
	}

	public String getB_team() {
		return b_team;
	}

	public String getScore() {
		return score;
	}

	public String getAlarm_title() {
		return alarm_title;
	}

	public String getAlarm_ringtone() {
		return alarm_ringtone;
	}

	public String getAlarm_volume() {
		return alarm_volume;
	}

	public String getAlarm_vibrate() {
		return alarm_vibrate;
	}

	public String getAlarm_auto_off_time() {
		return alarm_auto_off_time;
	}

	public String getSms() {
		return sms;
	}

	public String getVideo() {
		return video;
	}

	public String getRecord() {
		return record;
	}

	public String getHighlight() {
		return highlight;
	}

	public String getPower() {
		return power;
	}
	public String getNotification() {
		return notification;
	}
	public String getExplanation() {
		return explanation;
	}

	private Soccer call_alarm_values(String key_compare_date, String key_a_team, String key_b_team){
    	Soccer soccer = null;
    	SQLiteDatabase databse = null;		
		if(databse == null){
			databse = mContext.openOrCreateDatabase(DatabaseHelper.DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
		}
		String stringWhere = " WHERE " + DatabaseHelper.DATABASE_COLUMN_COMPARE_DATE + " = '" +key_compare_date+"' and "+ DatabaseHelper.DATABASE_COLUMN_A_TEAM +" = '"+key_a_team+"' and "+DatabaseHelper.DATABASE_COLUMN_B_TEAM+" = "+"'"+key_b_team+"'";		
		
		Cursor cursor = null;

		try {
			cursor = databse.rawQuery("SELECT "
					+ DatabaseHelper.DATABASE_COLUMN_ID + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ENABLED + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAL_DATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAL_TIME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_COMPARE_DATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SETTING_YEAR + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SETTING_MONTH + ", "
					+ DatabaseHelper.DATABASE_COLUMN_REAGUE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_STADIUM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_A_TEAM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_B_TEAM + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SCORE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_TITLE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_RINGTONE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_VOLUME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_VIBRATE + ", "
					+ DatabaseHelper.DATABASE_COLUMN_ALARM_AUTO_OFF_TIME + ", "
					+ DatabaseHelper.DATABASE_COLUMN_SMS + ", "
					+ DatabaseHelper.DATABASE_COLUMN_VIDEO + ", "
					+ DatabaseHelper.DATABASE_COLUMN_RECORD + ", "						
					+ DatabaseHelper.DATABASE_COLUMN_HIGHLIGHT + ", "						
					+ DatabaseHelper.DATABASE_COLUMN_POWER + ", "													
					+ DatabaseHelper.DATABASE_COLUMN_NOTIFICATION + ", "													
					+ DatabaseHelper.DATABASE_COLUMN_EXPLANATION + " " + " FROM "
					+ DatabaseHelper.DATABASE_TABLE_NAME + " " + stringWhere, null);
			if (cursor != null && cursor.moveToFirst()) {
				do {
					
					soccer = new Soccer(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20), cursor.getString(21), cursor.getString(22), cursor.getString(23));
					
				} while (cursor.moveToNext());
			}		
					
								
		} finally {
			if (cursor != null)
				cursor.close();
			if(databse != null){
				databse.close();
			}
		}
		return soccer;
    }
	
}
